import tui from '../../../common/httpRequest'

const db = wx.cloud.database()
// 校验规则
const rules = [{
    name: "sno",
    rule: ["required", "isNum", "minLength:6", "maxLength:9"],
    msg: ["请输入学号", "请输入正确的学号"]
}, {
    name: "name",
    rule: ["required", "isChinese", "minLength:2", "maxLength:6"],
    msg: ["请输入姓名", "姓名必须全部为中文", "姓名必须2个或以上字符", "姓名不能超过6个字符"]
}]
let form;

Page({
    data: {
        slideBarWidth: 300,
        slideBlockWidth: 40,
        reset: 0,
        // card数据
        sno: '',
        name: '',
        info: '',
        intro: '',
        // 
        formData: {},
        rules: rules,
        //
        flag: 0,
        cardif: false,
        //
        content: '查卡输入学号即可------捡卡人请填写完整信息方便失主尽快取得联系',
        newProduct: [{
                name: '云打印',
                tag: '可用',
                type: 1,
                isLabel: true,
                url: '../../../Packages/page1/pages/print/print',
            },
            {
                name: '手机报修',
                tag: '可用',
                type: 2,
                isLabel: true,
                url: '../../../Packages/page1/pages/iphoto/iphoto',
            },
            {
                name: '人工证件照',
                tag: '不可用',
                type: 1,
                isLabel: true,
                url: '../../../Packages/page1/pages/iphoto/iphoto',
            },
            {
                name: '等待',
                tag: '不可用',
                type: 1,
                isLabel: true,
                url: '../../../Packages/page1/pages/iphoto/iphoto',
            },
            {
                name: '等待',
                tag: '不可用',
                type: 1,
                isLabel: false,
                url: '../../../Packages/page1/pages/iphoto/iphoto',
            },
            {
                name: '等待',
                tag: '不可用',
                type: 2,
                isLabel: true
            }
        ],
    },

    onLoad: function (options) {
        setTimeout(() => {
            this.init()
        }, 50)
    },
    init() {
        let sys = wx.getSystemInfoSync()
        this.setData({
            slideBarWidth: sys.windowWidth - tui.rpx2px(60),
            slideBlockWidth: tui.rpx2px(90)
        })
    },
    detail: function (e) {
        let url = e.currentTarget.dataset.url
        tui.href(url)
    },
    success() {
        tui.toast("验证通过！")
        // let flag = this.data.flag
        // flag = 1
        this.setData({
            flag: 1
        })
    },

    // 挂失
    submit1: function (event) {
        if (!form) return;
        let {
            radioItems,
            formData,
            rules,
            ...rest
        } = this.data;
        //表单数据
        this.setData({
            formData: rest
        })
        form.validate(rest, this.data.rules).then(res => {
            let data = this.data
            let flag = this.data.flag
            if (flag) {
                db.collection('card').add({
                    // data字段表示需要新增的JSON数据
                    data: {
                        sno: data.sno,
                        name: data.name,
                        info: data.address,
                        intro: data.intro,
                        submitdate: db.serverDate()
                    }
                }).then(res => {
                    wx.showToast({
                        title: '挂失成功',
                        icon: 'success',
                        duration: 2000
                    })
                }).catch(console.error)
            } else {
                tui.toast("请先通过验证")
            }
        }).catch(errors => {
            console.log(errors)
        })
    },

    // 查卡
    submit2: function (event) {
        if (this.data.sno.length == 0) {
            if (!form) return;
            let {
                radioItems,
                formData,
                rules,
                ...rest
            } = this.data;
            form.validate(rest, this.data.rules)
        } else {
            let sno = this.data.sno
            let flag = this.data.flag
            if (flag) {
                // const _ = db.command
                db.collection('card').where({
                    sno: sno
                }).get().then(res => {
                    if (sno = res.data[0].sno) {
                        wx.showToast({
                            title: '查询成功',
                            icon: 'success',
                            duration: 2000
                        })
                        let cardif = this.data.cardif
                        cardif = true
                        this.setData({
                            cardif: cardif
                        })
                        let {
                            sno,
                            name,
                            info,
                            intro
                        } = res.data[0]
                        this.setData({
                            sno: sno,
                            name: name,
                            info: info,
                            intro: intro,
                        })

                    }
                }).catch(error => {
                    wx.showModal({
                        title: '查询失败',
                        content: '暂无记录'
                    });
                })
            } else {
                tui.toast("请先通过验证")
            }
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        form = this.selectComponent("#form")
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})